package com.fhminyi.GUI;

import com.fhminyi.dao.CommentDao;
import com.fhminyi.model.Comment;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

public class CommentManagementPanel extends JPanel {
    private JTable commentTable;
    private DefaultTableModel commentTableModel;
    private JButton deleteCommentButton;

    public CommentManagementPanel() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createTitledBorder("评论管理"));

        commentTableModel = new DefaultTableModel();
        commentTableModel.addColumn("评论ID");
        commentTableModel.addColumn("用户ID");
        commentTableModel.addColumn("商品ID");
        commentTableModel.addColumn("内容");
        commentTableModel.addColumn("评分");
        commentTableModel.addColumn("创建时间");

        commentTable = new JTable(commentTableModel);
        JScrollPane commentScrollPane = new JScrollPane(commentTable);
        add(commentScrollPane, BorderLayout.CENTER);

        deleteCommentButton = new JButton("删除选中的评论");
        deleteCommentButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int[] selectedRows = commentTable.getSelectedRows();
                if (selectedRows.length > 0) {
                    int selectedOption = JOptionPane.showConfirmDialog(CommentManagementPanel.this, "确定要删除选中的评论吗？", "确认删除", JOptionPane.YES_NO_OPTION);
                    if (selectedOption == JOptionPane.YES_OPTION) {
                        try {
                            CommentDao commentDao = new CommentDao();
                            for (int row : selectedRows) {
                                int commentId = (int) commentTableModel.getValueAt(row, 0);
                                commentDao.deleteComment(commentId);
                            }
                            loadComments();
                        } catch (SQLException ex) {
                            JOptionPane.showMessageDialog(CommentManagementPanel.this, "删除评论失败", "错误", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            }
        });
        add(deleteCommentButton, BorderLayout.SOUTH);
    }

    public void loadComments() {
        commentTableModel.setRowCount(0);
        try {
            CommentDao commentDao = new CommentDao();
            List<Comment> comments = commentDao.getAllComments();
            for (Comment comment : comments) {
                commentTableModel.addRow(new Object[]{comment.getCommentId(), comment.getUserId(), comment.getProductId(), comment.getContent(), comment.getRating(), comment.getCreateTime()});
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "加载评论失败", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}
